-- Player money functions --
local metaP=FindMetaTable("Player")

function metaP:AddMoney(amount)
	local current_cash=self:GetMoney()
	self:SetMoney(current_cash+amount)
end

function metaP:CheckFunds(amount)
	if (self:GetNWInt("money")-amount)<0 then
		self:ChatPrint("You don't have enough money.")
		return false
	else
		return true
	end
end

function metaP:GetRPName()
	return self:GetNWString("rpname")
end

function metaP:SetMoney(amount)
	self:SetNWInt("money",amount)
	self:ConCommand("SendCred")
end

function metaP:TakeMoney(amount)
	self:CheckFunds(amount)
	self:AddMoney(-amount)
end

function metaP:GetMoney()
	return self:GetNWInt("money")
end

function metaP:SetRPName(name)
	self:SetNWString("rpname",name)
	self:ConCommand("SendName")
	self:ChatPrint("You have changed your name to "..name)
end

function metaP:CanCP()
	return self:GetNWString("canCP")
end

function metaP:GetRank()
	return self:GetNWInt("rank")
end

-- Database functions --
-- Retrive player stats --
function sql_value_stats(ply)
	local steamID=ply:SteamID()
	local result=sql.Query("SELECT * FROM player_info WHERE unique_id='"..steamID.."'")
	if result then
		ply:SetNWString("unique_id",steamID)
		ply:SetNWInt("money",result[1]['money'])
		ply:SetNWString("rpname",result[1]['name'])
		ply:SetNWString("canCP",tostring(result[1]['canCP']))
		ply:SetNWInt("rank",result[1]['rank'])
		Msg("Details Loaded.\n")
	else
		Msg("Error loading details.\n")
		Msg(sql.LastError().."\n")
	end
end

-- Save player stats --
function saveStat(ply)
	local unique_id=tostring(ply:SteamID())
	local money=ply:GetMoney()
	local name=ply:GetNWString("rpname")
	local canCP=tostring(ply:CanCP())
	local rank=tostring(ply:GetRank())
	local query="UPDATE player_info SET money="..money..",name='"..name.."',canCP='"..canCP.."',rank='"..rank.."' WHERE unique_id='"..unique_id.."'"
	save=sql.Query(query)
end

-- Check that tables exist --
function tables_exist()
	if sql.TableExists("player_info") then
		Msg("Tables already exist!\n")
	else
		if (!sql.TableExists("player_info")) then
			query="CREATE TABLE player_info (unique_id varchar(255),name varchar(255),money int,canCP varchar(255),rank int)"
			result=sql.Query(query)
			if (sql.TableExists("player_info")) then
				Msg("Succes! player_info table created!\n")
			else
				Msg("Somthing went wrong with the player_info query!\n")
				Msg(sql.LastError().."\n")
			end	
		end
	end
end

-- Create a new player --
function new_player(SteamID,ply)
	sql.Query("INSERT INTO player_info (`unique_id`,`name`,`money`,`canCP`,`rank`) VALUES ('"..SteamID.."','"..ply:Nick().."','250','false','5')")
	result=sql.Query("SELECT unique_id,name,money,canCP FROM player_info WHERE unique_id='"..SteamID.."'")
	if (result) then
 		Msg("Player account created!\n")
		sql_value_stats(ply)
		ply:ChatPrint("Please choose an appropriate roleplay name.")
	else
		Msg("Something went wrong with creating a players skills!\n")
		Msg(sql.LastError().."\n")
	end
end

-- Check if the player exists in the table --
function player_exists(ply)
	steamID=tostring(ply:SteamID())
	result=sql.Query("SELECT unique_id FROM player_info WHERE unique_id='"..steamID.."'")
	if (result) then
		Msg("Existing player "..ply:Nick()..", loading details...\n")
		sql_value_stats(ply)
	else
		new_player(steamID,ply)
	end
end
function Initialize()
	tables_exist()
end
hook.Add("Initialize","Initialize",Initialize)